using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace PicturePage
{
    /// <summary>
    /// This Configuration class is basically just a set of properties with a couple of static methods to manage
    /// the serialization to and deserialization from a simple XML file.
    /// </summary>
    [Serializable]
    public class Configuration
    {
        private int _Version;
        public string _ConfigPassword;
        public bool _settingsLoaded;

        private string _templateFile;
        private string _templateInsertLocation;

        private string _serverAddress;
        private string _serverPort;
        private string _userName;
        private string _password;

        private int _columns;
        private int _rows;
        private bool _makeLinkedImages;
        private int _thumbnailWidth;
        private int _thumbnailHeight;
        private int _linkedImageWidth;
        private int _linkedImageHeight;
        private string _pictureAlign;

        public Configuration()
        {
            _Version = 1;
            _ConfigPassword = "SomeStringThatNoOneWillEverUseDefault";
            _settingsLoaded = false;

            _templateFile = Directory.GetCurrentDirectory() + "\\template.html";
            _templateInsertLocation = "<div id=\"rightColumn\">";

            _serverAddress = "";
            _serverPort = "21";
            _userName = "";
            _password = "";

            _columns = 3;
            _rows = 4;
            _makeLinkedImages = true;
            _thumbnailWidth = 180;
            _thumbnailHeight = 135;
            _linkedImageWidth = 1024;
            _linkedImageHeight = 768;
            _pictureAlign = "Center";
        }

        public static void Serialize(string file, Configuration c)
        {
            XmlSerializer xs = new XmlSerializer(c.GetType());
            StreamWriter writer = File.CreateText(System.Windows.Forms.Application.StartupPath + "\\" + file);

            string configpassword = c.ConfigPassword;
            if (c.ConfigPassword != "SomeStringThatNoOneWillEverUseDefault")
            {
                //pull the actual password off so that it's not stored in the file
                c.ConfigPassword = "SomeStringThatNoOneWillEverUse";
            }

            xs.Serialize(writer, c);

            //now put it back
            c.ConfigPassword = configpassword;

            writer.Flush();
            writer.Close();
        }
        
        public static Configuration Deserialize(string file)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Configuration));
            StreamReader reader = File.OpenText(file);
            Configuration c = (Configuration)xs.Deserialize(reader);
            reader.Close();
            return c;
        }

        public int Version
        {
            get { return _Version; }
            set { _Version = value; }
        }

        public string ConfigPassword
        {
            get { return _ConfigPassword; }
            set { _ConfigPassword = value; }
        }

        public bool SettingsLoaded
        {
            get { return _settingsLoaded; }
            set { _settingsLoaded = value; }
        }

        public string TemplateFile
        {
            get { return _templateFile; }
            set { _templateFile = value; }
        }

        public string TemplateInsertLocation
        {
            get { return _templateInsertLocation; }
            set { _templateInsertLocation = value; }
        }

        public string GetDecryptedServerAddress()
        {
            return Utility.DecryptMessage(_serverAddress, _ConfigPassword);
        }

        public void SetEncryptedServerAddress(string value)
        {
            _serverAddress = Utility.EncryptMessage(value, _ConfigPassword);
        }

        public string ServerAddress
        {
            get { return _serverAddress; }
            set { _serverAddress = value; }
        }

        public int GetDecryptedServerPort()
        {
            return int.Parse(Utility.DecryptMessage(_serverPort, _ConfigPassword));
        }

        public void SetEncryptedServerPort(string value)
        {
            _serverPort = Utility.EncryptMessage(value, _ConfigPassword);
        }

        public string ServerPort
        {
            get { return _serverPort; }
            set { _serverPort = value; }
        }

        public string GetDecryptedUserName()
        {
            return Utility.DecryptMessage(_userName, _ConfigPassword);
        }

        public void SetEncryptedUserName(string value)
        {
            _userName = Utility.EncryptMessage(value, _ConfigPassword);
        }

        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        public string GetDecryptedPassword()
        {
            return Utility.DecryptMessage(_password, _ConfigPassword);
        }

        public void SetEncryptedPassword(string value)
        {
            _password = Utility.EncryptMessage(value, _ConfigPassword);
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public int Columns
        {
            get { return _columns; }
            set { _columns = value; }
        }

        public int Rows
        {
            get { return _rows; }
            set { _rows = value; }
        }

        public bool MakeLinkedImages
        {
            get { return _makeLinkedImages; }
            set { _makeLinkedImages = value; }
        }

        public int ThumbnailWidth
        {
            get { return _thumbnailWidth; }
            set { _thumbnailWidth = value; }
        }

        public int ThumbnailHeight
        {
            get { return _thumbnailHeight; }
            set { _thumbnailHeight = value; }
        }

        public int LinkedImageWidth
        {
            get { return _linkedImageWidth; }
            set { _linkedImageWidth = value; }
        }

        public int LinkedImageHeight
        {
            get { return _linkedImageHeight; }
            set { _linkedImageHeight = value; }
        }

        public string PictureAlign
        {
            get { return _pictureAlign; }
            set { _pictureAlign = value; }
        }
    }
}
